package com.dykyi.SimpleIteration;
import javax.swing.table.AbstractTableModel;

public class TableExModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	
	private int n = 0;
	private int m = 0;
	private double[][] matrix = null;
	
	public int getColumnCount() {
		return m;
	}

	public int getRowCount() {
		return n;
	}
	public Object getValueAt(int i, int j) {
		if (matrix == null) {
			return null;
		}
		return matrix[i][j];
	}
	
	//method to adding values to table
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		if (aValue instanceof String) {
			String stringValue = (String) aValue;
			matrix[rowIndex][columnIndex] = Double.valueOf(stringValue);
		} else {
			matrix[rowIndex][columnIndex] = (Integer) aValue;
		}
	}
	//method to set coloumn name
	public String getColumnName(int c) {
		if (m == 1) {
			return " ";
		} else {
			return " * X" + (c + 1);
		}
	}
	//method to primission edit table
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return true;
	}
	//method to fill table
	public void fill(int n, int m) {
		this.n = n;
		this.m = m;
		matrix = new double[n][m];
		fireTableStructureChanged();
	}
	//method to return matrix
	public double[][] getMatr() {
		return matrix;
	}
}
